/**
 * 功能:车间物料盘点 控制器类
 * 开发人员:chengd
 * 创建时间:2012/5/12 23:02:31
 */
package com.towker.frame.controller.sell;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.controller.BaseController;
import com.towker.frame.model.sell.GoodsCheck;
import com.towker.frame.service.sell.GoodsCheckService;
import com.towker.frame.share.sys.user.UserShare;

//车间物料盘点
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/sell")
public class GoodsCheckController extends BaseController
{
	private GoodsCheckService service;

	//业务对象注入
	public void setGoodsCheckService(GoodsCheckService service)
	{
		this.service = service;
	}

	//添加
	@RequestMapping("/addGoodsCheck{viewName}")
	public ModelAndView addGoodsCheck(HttpServletRequest request, HttpServletResponse response, GoodsCheck po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.addObject("check_sn", service.getNewCheckSN());
			result.setViewName("/manage/sell/addGoodsCheck.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				po.setCreater_code(UserShare.getCurrentUserCode(request));
				po.setCheck_sn(service.getNewCheckSN());
				po.setAuthCoCode(UserShare.getCurrentUserCoCode(request));
				//po.setAuthDepCode(UserShare.getCurrentUserDepCode(request));
				service.add(po);
				writer.write("添加车间物料盘点成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加车间物料盘点失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delGoodsCheck")
	public void delGoodsCheck(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updGoodsCheck{viewName}")
	public ModelAndView updGoodsCheck(HttpServletRequest request, HttpServletResponse response, GoodsCheck po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			result.setViewName("/manage/sell/updGoodsCheck.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.update(po);
				writer.write("修改车间物料盘点成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改车间物料盘点失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getGoodsCheck")
	public ModelAndView getGoodsCheck(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		//只查询当前用户所属部门
		map.put("authCoCode", UserShare.getCurrentUserCoCode(request));
		//map.put("authDepCode", UserShare.getCurrentUserDepCode(request));
		PageModel<GoodsCheck> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/sell/getGoodsCheck.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getGoodsCheckById")
	public ModelAndView getGoodsCheckById(HttpServletRequest request, HttpServletResponse response)
	{
		GoodsCheck po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.setViewName("/manage/sell/getGoodsCheckById.jsp");
		return result;
	}
}
